/**
 * Copyright (C) 2017 - 2020 https://github.com/joewee
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.bifrost.web.controller;

import com.bifrost.annotation.Authorization;
import com.bifrost.annotation.CurrentStore;
import com.bifrost.common.util.HttpRequestUtils;
import com.bifrost.entity.CountryArea;
import com.bifrost.entity.Media;
import com.bifrost.entity.MerchantStore;
import com.bifrost.entity.ServicePrice;
import com.bifrost.model.AreaModel;
import com.bifrost.common.model.ResultModel;
import com.bifrost.model.ServiceModel;
import com.bifrost.model.StoreModel;
import com.bifrost.service.MediaService;
import com.bifrost.service.StoreService;
import com.bifrost.util.TransformUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author joewee
 * @version 1.0.0
 * @Date 2017/11/1 11:42
 */
@RestController
@RequestMapping("/api/store")
@Api("门店相关api接口")
public class StoreController {
    @Autowired
    StoreService storeService;
    @Autowired
    MediaService mediaService;

    @ApiOperation("查询所有门店")
    @GetMapping("/list")
    public ResultModel getStores() {
        List<MerchantStore> stores = storeService.getMerchantStores();
        if (stores.isEmpty()) {
            return ResultModel.error("未找到数据");
        }
        List<StoreModel> models = new ArrayList<>(stores.size());
        List<AreaModel> areas = new ArrayList<>(stores.size());
        stores.forEach(merchantStore -> {
            models.add(TransformUtils.transformToStoreModel(merchantStore));
            CountryArea countryArea = merchantStore.getStoreDistrict();
            AreaModel info = new AreaModel(countryArea.getId(), countryArea.getName());
            areas.add(info);
        });
        //去重
        List<AreaModel> distinctAreas = areas.stream().distinct().collect(Collectors.toList());
        //排序
        List<StoreModel> sortedModel = models.stream().sorted(Comparator.comparing(StoreModel::getSortOrder)).collect(Collectors.toList());
        return ResultModel.ok("成功").put("areas", distinctAreas).put("stores", sortedModel);
    }

    @ApiOperation("查询某个区所有门店")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "area", dataType = "Long", value = "地区ID", required = true)
    })
    @GetMapping("/search")
    public ResultModel getStoresWithArea(@RequestParam Long area) {
        List<MerchantStore> stores = storeService.getMerchantStores(area);
        if (stores.isEmpty()) {
            return ResultModel.error("未找到数据");
        }
        List<StoreModel> models = new ArrayList<>(stores.size());
        stores.stream().forEach(merchantStore -> models.add(TransformUtils.transformToStoreModel(merchantStore)));
        return ResultModel.ok("成功").put("stores", models);
    }

    @ApiOperation("获取门店详情")
    @ApiImplicitParam(paramType = "query", name = "merchantId", dataType = "Long", value = "门店ID", required = true)
    @GetMapping("/detail")
    public ResultModel getStoreDetail(@RequestParam Long merchantId) {
        MerchantStore store = storeService.getMerchantStore(merchantId);
        if (store == null) {
            return ResultModel.error("未找到数据");
        }
        List serviceModelList = new ArrayList<ServiceModel>();
        List<ServicePrice> servicePriceList = store.getServicePrices();
        Map serviceMap = new HashMap(4);
        servicePriceList.forEach(servicePrice -> {
            if (servicePrice.getIsAvailable()) {
                ServiceModel serviceModel = TransformUtils.transformToServiceModel(servicePrice);
                serviceModel.setServicePrice(servicePrice.getServicePrice());
                serviceModelList.add(serviceModel);
            }
        });
        List<String> banners = new ArrayList<String>();
        if (store.getAlt1() != null) {
            Media media = mediaService.getMediaById(store.getAlt1());
            if (media != null) {
                banners.add(HttpRequestUtils.getUrl(media.getUrl()));
            }
        }
        if (store.getAlt2() != null) {
            Media media = mediaService.getMediaById(store.getAlt2());
            if (media != null) {
                banners.add(HttpRequestUtils.getUrl(media.getUrl()));
            }
        }
        if (store.getAlt3() != null) {
            Media media = mediaService.getMediaById(store.getAlt3());
            if (media != null) {
                banners.add(HttpRequestUtils.getUrl(media.getUrl()));
            }
        }
        if (store.getAlt4() != null) {
            Media media = mediaService.getMediaById(store.getAlt4());
            if (media != null) {
                banners.add(HttpRequestUtils.getUrl(media.getUrl()));
            }
        }
        return ResultModel.ok("成功")
                .put("urls", banners)
                .put("store", TransformUtils.transformToStoreModel(store))
                .put("serviceModelList", serviceModelList);
    }

    @ApiOperation("获取门店基本信息")
    @ApiImplicitParam(paramType = "query", name = "merchantId", dataType = "Long", value = "门店ID", required = true)
    @GetMapping("/baseInfo")
    public ResultModel baseInfo(@RequestParam Long merchantId) {
        MerchantStore store = storeService.getMerchantStore(merchantId);
        return ResultModel.ok("成功")
                    .put("storePhone", store.getStorePhone())
                    .put("storeName", store.getStoreName())
                    .put("storeDesc", store.getDescription());
    }
    @Authorization
    @ApiOperation("获取门店基本信息")
    @GetMapping("/storeInfo")
    public ResultModel storeInfo(@CurrentStore Long storeId) {
        return storeService.getMerchantStoreInfo(storeId);
    }
}
